<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>getter/setter方法 | 封装</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../109.html" />
    
    
    <link rel="prev" href="../index.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="1" data-basepath=".." data-revision="1442487840127">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         封装
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="1" data-path="gettersetter/README.html">
            
                
                    <a href="../gettersetter/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         getter/setter方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="109.html">
            
                
                    <a href="../109.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         自定义代码段
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="333.html">
            
                
                    <a href="../333.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         点语法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="self/README.html">
            
                
                    <a href="../self/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         Self关键字
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="8.html">
            
                
                    <a href="../8.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         继承基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="14.html">
            
                
                    <a href="../14.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         继承相关特性
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="super/README.html">
            
                
                    <a href="../super/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         Super关键字
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="15.html">
            
                
                    <a href="../15.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         多态基本概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="16.html">
            
                
                    <a href="../16.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         多态的实现
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="876.html">
            
                
                    <a href="../876.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         实例变量修饰符
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="description/README.html">
            
                
                    <a href="../description/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         description方法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="12" data-path="11.html">
            
                
                    <a href="../11.html">
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                         总结
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="13" data-path="12.html">
            
                
                    <a href="../12.html">
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                         工作
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="14" data-path="chapter/README.html">
            
                
                    <a href="../chapter/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                         Chapter
                    </a>
                
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >封装</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_527">
                    
                        <h1 id="gettersetter">getter/setter方法</h1>
<h2 id="">本小节知识:</h2>
<ol>
<li>setter方法</li>
<li>getter方法</li>
<li>getter/setter方法注意点</li>
<li>练习</li>
</ol>
<hr>
<h2 id="1setter">1.setter方法</h2>
<ul>
<li><p>作用：用来设置成员变量，可以在方法里面过滤掉一些不合理的值</p>
</li>
<li><p>命名规范：</p>
<ul>
<li>必须是对象方法</li>
<li>返回值类型为void</li>
<li>方法名必须以set开头，而且后面跟上成员变量名去掉”_” 首字母必须大写</li>
<li>必须提供一个参数，参数类型必须与所对应的成员变量的类型一致</li>
<li>形参名称和成员变量去掉下划线相同</li>
</ul>
</li>
<li><p>举例：</p>
<pre><code class="lang-objc">如：如果成员变量为<span class="hljs-keyword">int</span> _age 那么与之对应seter方法为
-(<span class="hljs-keyword">void</span>) setAge: (<span class="hljs-keyword">int</span>) age;
</code></pre>
</li>
<li><p>setter方法的好处</p>
<ul>
<li>不让数据暴露在外,保证了数据的安全性</li>
<li>对设置的数据进行过滤</li>
</ul>
</li>
</ul>
<hr>
<h2 id="2getter">2.getter方法</h2>
<ul>
<li><p>作用：为调用者返回对象内部的成员变量的值</p>
</li>
<li><p>命名规范：</p>
<ul>
<li>必须是对象方法</li>
<li>必须有返回值,返回值的类型和成员变量的类型一致</li>
<li>方法名必须是成员变量去掉下划线</li>
<li>一定是没有参数的</li>
</ul>
</li>
<li><p>举例</p>
</li>
</ul>
<pre><code class="lang-objc">如：如果成员变量为<span class="hljs-keyword">int</span> _age 那么与之对应geter方法为
- (<span class="hljs-keyword">int</span>) age;
</code></pre>
<ul>
<li>getter方法的优点：<ul>
<li>可以让我们在使用getter方法获取数据之前,对数据进行加工；</li>
<li>比如双十一活动，我们希望对全线商品的价格在原来的价格基础上打五折，那么我们只要去改成品类的价格的getter方法就可以了，让他返回的值为价格 * 0.5</li>
</ul>
</li>
</ul>
<hr>
<h2 id="3gettersetter">3.getter/setter方法注意</h2>
<ul>
<li><p>在实际的开发中,不一定set和get方法都会提􏰀供,如果内部的成员变量,比如学生的学号或计算出来的数据。这样的数据只允许外界读取，但是不允许修改的情况,则通常只提􏰀供get方法而不􏰀提供set方法
。</p>
</li>
<li><p>成员变量名的命名以下划线开头,get方法名不需要带下划线</p>
</li>
<li><p>成员变量名使用下划线开头有两个好处</p>
<ul>
<li>与get方法的方法名区分开来</li>
<li>可以和一些其他的局部变量区分开来,下划线开头的变量,通常都是类的成员变量。当我看到以下划线开头变量，那么他一定是成员变量</li>
</ul>
</li>
</ul>
<hr>
<h2 id="4">4.练习</h2>
<ul>
<li>设计一个K线类, 有三个属性, 最高价格, 最低价格, 平均价格. 要求可以设置和获取这三个属性</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../index.html" class="navigation navigation-prev " aria-label="Previous page: 封装"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../109.html" class="navigation navigation-next " aria-label="Next page: 自定义代码段"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
